package proyecto3;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

/**
 *
 * @author Kathy
 */
public class VentanaBus extends JFrame {

    public VentanaBus(String titulo) {
        super(titulo);
        configuracionInicial();
        setResizable(true);
        ajustarComponentes(getContentPane());
        ajustarMenus();
        ajustarEventos();
    }

    private void configuracionInicial() {
        setSize(200, 400);
        setLocationRelativeTo(null);
        //pack();
        setMinimumSize(getSize());
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

    }

    private void ajustarComponentes(Container c) {

        c.setLayout(new BorderLayout());

        c.add(BorderLayout.CENTER, panel = new JPanel() {

            @Override
            public void paintComponent(Graphics bg) {

                super.paintComponent(bg);
                Graphics2D g = (Graphics2D) bg;

//                Dimension tamano = getSize();
//                ImageIcon imagenFondo = new ImageIcon(getClass().getResource("imagen/bus1.png"));
//                g.drawImage(imagenFondo.getImage(), 0, 0, tamano.width, tamano.height, null);
//                setOpaque(false);
            }
        });

        panel.setLayout(new GridBagLayout());

        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);

        //EL ID
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        panel.add(new JLabel("Id: "), gbc);

        gbc.gridx = 1;
        gbc.gridy = 0;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 2;
        panel.add(id = new JTextField(), gbc);

        //LA PLACA
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        panel.add(new JLabel("Placa: "), gbc);

        gbc.gridx = 1;
        gbc.gridy = 1;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 2;
        panel.add(placa = new JTextField(), gbc);

        //EL MODELO
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        panel.add(new JLabel("Modelo: "), gbc);

        gbc.gridx = 1;
        gbc.gridy = 2;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 2;
        panel.add(modelo = new JTextField(), gbc);

        //LA MARCA
        gbc.gridx = 0;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        panel.add(new JLabel("Marca: "), gbc);

        gbc.gridx = 1;
        gbc.gridy = 3;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 2;
        panel.add(marca = new JTextField(), gbc);

        //LA CAPACIDAD
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        panel.add(new JLabel("Capacidad: "), gbc);

        gbc.gridx = 1;
        gbc.gridy = 4;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 2;
        panel.add(capacidad = new JTextField(), gbc);
        
        //LA DESCRIPCION
        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.NONE;
        gbc.weightx = 0;
        panel.add(new JLabel("Descripcion: "), gbc);

        gbc.gridx = 1;
        gbc.gridy = 5;
        gbc.fill = GridBagConstraints.HORIZONTAL;
        gbc.weightx = 2;
        panel.add(descripcion = new JTextField(), gbc);

    }

    private void ajustarMenus() {
        menuPrincipal = new JMenuBar();

        menuArchivo = new JMenu("Archivo");
        itemSalir = new JMenuItem("Salir");
        menuArchivo.add(itemSalir);
        menuPrincipal.add(menuArchivo);

        menuAyuda = new JMenu("Ayuda");
        itemAcercaDe = new JMenuItem("Acerca de...");
        menuAyuda.add(itemAcercaDe);
        menuPrincipal.add(menuAyuda);

        setJMenuBar(menuPrincipal);
    }

    private void ajustarEventos() {

        itemSalir.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                cerrarAplicacion();
            }
        });

        itemAcercaDe.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                mostrarAcercaDe();
            }
        });

        this.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                cerrarAplicacion();
            }
        });
    }

    private void cerrarAplicacion() {
        int opt = JOptionPane.showConfirmDialog(this,
                "¿Desea cerrar la aplicación?", "Cerrar Aplicacion",
                JOptionPane.YES_NO_OPTION);
        if (opt == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }

    public void mostrarAcercaDe() {
        JOptionPane.showMessageDialog(null, INFO_ACERCADE,
                String.format("Acerca de %s..", DESCRIPCION_APLICACION),
                JOptionPane.INFORMATION_MESSAGE);
    }

    public void init() {
        setVisible(true);
    }

    //PARA EL FORMULARIO    
    private JPanel panel;
    private JTextField id;
    private JTextField placa;
    private JTextField modelo;
    private JTextField marca;
    private JTextField capacidad;
    private JTextField descripcion;

    //PARA AJUSTAR MENUS
    private JMenuBar menuPrincipal;
    private JMenu menuArchivo;
    private JMenuItem itemSalir;
    private JMenu menuAyuda;
    private JMenuItem itemAcercaDe;

    private static final String INFO_ACERCADE = "\u00A92014 Programación 3\nUniversidad Nacional UNA";
    public static final String DESCRIPCION_APLICACION = "Proyecto#3";

}
